package com.anjiplus.template.gaea.business.util;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;

public class MacUtil {
    public static void main(String[] args) throws Exception {
//        InetAddress ia = InetAddress.getLocalHost();
        String mac = getMACAddress();
        System.out.println(mac);
//        String license = readLicense();
//        System.out.println("license:" + license);
        String s = DesUtil.getEncryptString("{mac:[C4-75-AB-3B-10-D7,44-8A-5B-AE-29-06],untilDate:2099-12-12}");
        System.out.println("加密后：" + s);
//        String s1 = DesUtil.getDecryptString(license);
//        System.out.println("解密后：" + s1);
//        JSONObject jsonObject = new JSONObject(s1);
//        System.out.println(jsonObject.get("mac"));
//        System.out.println(jsonObject.get("untilDate"));
    }

    private static String readLicense(){
        String s = "";
        StringBuffer content = new StringBuffer();
        try {
            InputStreamReader in = new InputStreamReader(new FileInputStream(new File("LICENSE")), "UTF-8");
            BufferedReader br = new BufferedReader(in);
            while ((s = br.readLine()) != null) {
                content = content.append(s);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return content.toString();
    }

    public static String getMACAddress(){
        StringBuffer sb = new StringBuffer();
        try {
            InetAddress ia = InetAddress.getLocalHost();
            // 获得网络接口对象（即网卡），并得到mac地址，mac地址存在于一个byte数组中。
            byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
            // 下面代码是把mac地址拼装成String
            if(mac == null){
                return "";
            }

            for (int i = 0; i < mac.length; i++) {
                if (i != 0) {
                    sb.append("-");
                }
                // mac[i] & 0xFF 是为了把byte转化为正整数
                String s = Integer.toHexString(mac[i] & 0xFF);
                sb.append(s.length() == 1 ? 0 + s : s);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
// 把字符串所有小写字母改为大写成为正规的mac地址并返回
        return sb.toString().toUpperCase();
    }
}